package jgl.vajra.operator.part.home.activity;

import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import jgl.vajra.operator.R;
import jgl.vajra.operator.databinding.ActivityCityChoiceBinding;
import jgl.vajra.operator.event.CityChoiceEvent;
import jgl.vajra.operator.part.home.viewModel.CityChoiceViewModel;
import jgl.vajra.operator.widget.databindingadapter.BaseBindingItemPresenter;
import jgl.vajra.operator.widget.databindingadapter.SingleTypeBindingAdapter;
import jgl.vajra.operator.widget.manager.SPManager;
import jgl.vajra.operator.widget.mvvm.factory.CreateViewModel;
import jgl.vajra.operator.widget.mvvm.view.BaseMVVMActivity;

@CreateViewModel(CityChoiceViewModel.class)
public class CityChoiceActivity extends BaseMVVMActivity<CityChoiceViewModel, ActivityCityChoiceBinding>implements BaseBindingItemPresenter<String> {
    @Override
    public int getLayoutId() {
        return R.layout.activity_city_choice;
    }

    @Override
    public void initView() {
        super.initView();

        List<String> list= (ArrayList) getIntent().getSerializableExtra("list");


        // 不可嵌套滑动
        mBinding.recyclerView.setNestedScrollingEnabled(false);
        // 取消head 的 recycleView的焦点
        mBinding.recyclerView.setFocusableInTouchMode(false);

        FlexboxLayoutManager flManager = new FlexboxLayoutManager(this);
        //
        flManager.setFlexWrap(FlexWrap.WRAP);
        //设置主轴的方向
        flManager.setFlexDirection(FlexDirection.ROW);
        //设置布局管理器
        mBinding.recyclerView.setLayoutManager(flManager);

        SingleTypeBindingAdapter adapter = new SingleTypeBindingAdapter(this, list, R.layout.item_city_choice);
        mBinding.recyclerView.setAdapter(adapter);
        adapter.setItemPresenter(this);

    }

    @Override
    public void initData() {
        super.initData();
    }

    @Override
    public void onItemClick(int position, String itemData) {
        SPManager.HomeData.saveCityData(itemData);

        EventBus.getDefault().post(new CityChoiceEvent(position));
        finish();
    }
}